﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.ServiceModel;
using System.Data;

namespace MM.Common.Base
{
    public class QueryReportServer
    {
        // Fields
        private StringBuilder errorMessage = new StringBuilder();
        private bool isRegister;
        private string nodeMac;
        private QueryReportClient queryReport;
        private string reportRegisterCode = "";

        // Methods
        private bool CheckQuery()
        {
            try
            {
                if (this.queryReport.State != CommunicationState.Opened)
                {
                    if (this.queryReport.State != CommunicationState.Created)
                    {
                        this.queryReport = new QueryReportClient();
                    }
                    this.queryReport.Open();
                }
                return (this.queryReport.State == CommunicationState.Opened);
            }
            catch (Exception exception)
            {
                this.errorMessage.Append(exception.Message);
                return false;
            }
        }

        protected void ClearErrorMessage()
        {
            this.errorMessage.Remove(0, this.errorMessage.Length);
        }

        public bool GetReportInfo(string filename, ref byte[] rptformat, ref string[] parmname, ref string[] dsname, ref string[] dsquery, ref string[] codemodule)
        {
            bool flag = true;
            this.ClearErrorMessage();
            if (!this.CheckQuery())
            {
                this.errorMessage.Append("查询服务不能使用!");
                return false;
            }
            if (this.reportRegisterCode == "")
            {
                this.errorMessage.Append("报表注册服务没有成功，查询不能继续！");
                return false;
            }
            try
            {
                string error = "";
                string s = this.queryReport.GetReportInfo(filename, ref parmname, ref dsname, ref dsquery, ref codemodule, ref error);
                if (s == string.Empty)
                {
                    this.errorMessage.Append("报表服务出错:" + error);
                    return false;
                }
                rptformat = Convert.FromBase64String(s);
            }
            catch (Exception exception)
            {
                this.errorMessage.Append(exception.Message);
                flag = false;
            }
            return flag;
        }

        public bool GetXtraReportInfo(string filename, ref byte[] rptformat)
        {
            bool flag = true;
            this.ClearErrorMessage();
            if (!this.CheckQuery())
            {
                this.errorMessage.Append("查询服务不能使用!");
                return false;
            }
            if (this.reportRegisterCode == "")
            {
                this.errorMessage.Append("报表注册服务没有成功，查询不能继续！");
                return false;
            }
            try
            {
                string error = "";
                string xtraReportInfo = this.queryReport.GetXtraReportInfo(filename, ref error);
                if (xtraReportInfo == string.Empty)
                {
                    this.errorMessage.Append("报表服务出错:" + error);
                    return false;
                }
                rptformat = Convert.FromBase64String(xtraReportInfo);
            }
            catch (Exception exception)
            {
                this.errorMessage.Append(exception.Message);
                flag = false;
            }
            return flag;
        }

        protected string GzipDeCompress(byte[] bytInput)
        {
            StringBuilder builder = new StringBuilder();
            int num = 0;
            byte[] buffer = new byte[0x1000];
            try
            {
                Stream stream = new GZipStream(new MemoryStream(bytInput), CompressionMode.Decompress);
                while (true)
                {
                    int count = stream.Read(buffer, 0, buffer.Length);
                    if (count <= 0)
                    {
                        break;
                    }
                    num += count;
                    builder.Append(Encoding.Unicode.GetString(buffer, 0, count));
                }
                stream.Close();
            }
            catch
            {
            }
            return builder.ToString();
        }

        public bool Register(string mac)
        {
            this.isRegister = true;
            this.ClearErrorMessage();
            try
            {
                string error = "";
                this.queryReport = new QueryReportClient();
                this.queryReport.Open();
                this.nodeMac = mac;
                this.reportRegisterCode = this.queryReport.GetRegisterCode(mac, ref error);
                if (this.reportRegisterCode == "")
                {
                    this.errorMessage.Append(error);
                    this.isRegister = false;
                }
            }
            catch (Exception exception)
            {
                this.errorMessage.Append(exception.Message);
                this.isRegister = false;
            }
            return this.isRegister;
        }

        public bool Retrieve(string report_code, string data_set, object[] parm_value, string query, ref DataTable dt)
        {
            bool flag = true;
            this.ClearErrorMessage();
            if (!this.CheckQuery())
            {
                this.errorMessage.Append("查询服务不能使用!");
                return false;
            }
            if (this.reportRegisterCode == "")
            {
                this.errorMessage.Append("报表注册服务没有成功，查询不能继续！");
                return false;
            }
            string[] strArray = new string[parm_value.Length];
            string[] strArray2 = new string[parm_value.Length];
            for (int i = 0; i < parm_value.Length; i++)
            {
                strArray[i] = parm_value[i].ToString();
                strArray2[i] = parm_value[i].GetType().Name;
            }
            try
            {
                string error = " ";
                string s = this.queryReport.Retrieve(this.reportRegisterCode, this.nodeMac, report_code, data_set, query, strArray, strArray2, ref error);
                if (s == string.Empty)
                {
                    this.errorMessage.Append("报表服务出错:" + error);
                    return false;
                }
                byte[] bytInput = Convert.FromBase64String(s);
                s = this.GzipDeCompress(bytInput);
                dt.ReadXml(new StringReader(s.Trim()));
                dt.AcceptChanges();
            }
            catch (Exception exception)
            {
                this.errorMessage.Append(exception.Message);
                flag = false;
            }
            return flag;
        }

        // Properties
        public string ErrorMessage
        {
            get
            {
                return this.errorMessage.ToString();
            }
        }

        public bool IsRegister
        {
            get
            {
                return this.isRegister;
            }
        }
    }
}
